package com.starbaba.stat.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.stat.model.StatCoreBehaviorModel;
import com.starbaba.stat.service.StatCoreBehaviorDayService;

@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class StatCoreBehaviorAction extends BaseAction implements ModelDriven<StatCoreBehaviorModel>{

	private StatCoreBehaviorModel model;
	private StatCoreBehaviorDayService service;
	
	
	private InputStream input;

	private String fileName;
	
	public InputStream getInput() {
		return input;
	}

	public void setInput(InputStream input) {
		this.input = input;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	@Autowired
	@Qualifier("statCoreBehaviorDayServiceImpl")
	public void setService(StatCoreBehaviorDayService service) {
		this.service = service;
	}

	public String showPage() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			service.showPage(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ActionSupport.SUCCESS;
	}
	
	public void queryList(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		
		String model = (String) paramsMap.get("model");
		try {
			List<StatCoreBehaviorModel> list = new ArrayList<StatCoreBehaviorModel>();
			if ("week".equals(model)) {
				list = service.getListWeek(start, length, pageResponse, paramsMap);
			} else if ("month".equals(model)) {
				list = service.getListMonth(start, length, pageResponse, paramsMap);
			} else {
				list = service.getListDay(start, length, pageResponse, paramsMap);
			}
			
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String exportCVS() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		String value = exportCVS(service.getExcelListDay(paramsMap));
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}
	
	public String exportCVS(List<StatCoreBehaviorModel> list) {
		StringBuilder builder = new StringBuilder();
		builder.append("日期,平台,产品,渠道,新增数,新增时间注册用户数,新增时间申请贷款用户数,新增时间放贷成功数,新增时间放贷成功结算金额,"
				+ "放贷时间放贷成功数,放贷时间放贷成功结算金额,新增时间注册率,新增时间放贷成功率\r\n");
		for (StatCoreBehaviorModel vo : list) {
			builder.append(vo.getDate()).append(",");
			builder.append(vo.getPlatform()).append(",");
			builder.append(vo.getPrdId()).append(",");
			builder.append(vo.getChannel()).append(",");
			builder.append(vo.getIncreased()).append(",");
			builder.append(vo.getNewRegisterde()).append(",");
			builder.append(vo.getNewApplyCount()).append(",");
			builder.append(vo.getNewLoanSuccess()).append(",");
			builder.append(vo.getNewSuccessSettle()).append(",");
			builder.append(vo.getLoanLoanSuccess()).append(",");
			builder.append(vo.getLoanSuccessSettle()).append(",");
			builder.append(vo.getNewRegisterdeRate()).append(",");
			builder.append(vo.getNewLoanSuccessRate()).append(",");
			builder.append("\r\n");
		}
		return builder.toString();
	}
	
	
	
	
	@Override
	public StatCoreBehaviorModel getModel() {
		if (model == null) {
			model = new StatCoreBehaviorModel();
		}
		return model;
	}

}
